﻿using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using fps_arrow.Properties;
using System.Runtime.InteropServices;
namespace fps_arrow
{
    public partial class Form1 : Form 
    {
        private Arrow arrowWin = null;
        public Form1()
        {
            InitializeComponent();
            this.InitArrowOffset();
        }
        private void InitArrowOffset()
        {
            // 从配置中读取
            this.ArrowOffsetX.Text = Settings.Default.OffsetX;
            this.ArrowOffsetY.Text = Settings.Default.OffsetY;
            this.lineWidth.Text = Settings.Default.LineWidth;
            this.lineLen.Text = Settings.Default.LineLen;
            this.ArrowSpace.Text = Settings.Default.ArrowSpace;
            this.TC.Text = Settings.Default.TC;
        }
        private void SaveArrowOffset()
        {
            Settings.Default.OffsetX = this.ArrowOffsetX.Text;
            Settings.Default.OffsetY = this.ArrowOffsetY.Text;
            Settings.Default.LineLen = this.lineLen.Text;
            Settings.Default.LineWidth = this.lineWidth.Text;
            Settings.Default.ArrowSpace = this.ArrowSpace.Text;
            Settings.Default.TC = this.TC.Text;
            Settings.Default.Save();
        }
        private bool CheckArrowOffset(string offsetValue)
        {

            if (offsetValue.Equals(""))
            {
                return false;
            }

            try
            {
                int.Parse(offsetValue);
            }
            catch
            {
                return false;
            }

            return true;
        }
    
        private void OnRunButtonClick(object sender, EventArgs e)
        {
            if (this.arrowWin != null)
            {
                this.CloseArrowWindow();
            }
            else
            {
                if (!this.CheckArrowOffset(this.ArrowOffsetX.Text))
                {
                    return;
                }


                if (!this.CheckArrowOffset(this.ArrowOffsetY.Text))
                {
                    return;
                }

                if (!this.CheckArrowOffset(this.ArrowOffsetY.Text))
                {
                    return;
                }

                this.SaveArrowOffset();
                this.CreateArrowWindow();
            }
        }

        private void onSourceCodeLinkClick(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("iexplore.exe", "https://gitee.com/tidys/FPS-Arrow");
        }
        private void OnUseDocClick(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("iexplore.exe", "https://gitee.com/tidys/FPS-Arrow/blob/master/doc/Use.md");
        }
        private void OnOffsetValueInput(object sender, KeyPressEventArgs e)
        {
            char key = e.KeyChar;

            if (key == 0x2D)
            {
                // 输入的是减号,转换
                e.Handled = false;
            }
            else if (key == 8) {
                // 删除键
                e.Handled = false;
            }
            else
            {
                if (Char.IsNumber(key) && !Char.IsPunctuation(key) && !Char.IsControl(key))
                {
                    e.Handled = false;

                }
                else
                {
                    e.Handled = true;

                }
            }
            TextBox textBox = (TextBox)sender;
            if (textBox.Text.Length>=5){
                e.Handled = true;
            }
        }
        private void OnAppKeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void OnAppKeyDown(object sender, KeyEventArgs e)
        {

        }
        private void CreateArrowWindow()
        {
            this.CloseArrowWindow();
            this.arrowWin = new Arrow();
            this.arrowWin.Show();
            this.runButton.Text = "关闭准星(ESC)";
        }
        private void CloseArrowWindow()
        {
            if (this.arrowWin != null)
            {
                this.arrowWin.Close();
                this.arrowWin = null;
                this.runButton.Text = "创建准星";
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if(keyData == Keys.Escape)
            {
                this.CloseArrowWindow();
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void OnOffsetValueOver(object sender, KeyEventArgs e)
        {
            TextBox textBox =( (TextBox)sender);
            if (this.CheckArrowOffset(textBox.Text))
            {
                int sceneBoard = 40;
                Rectangle screenArea = Screen.GetBounds(this);

                int maxX =screenArea.Width / 2 - sceneBoard;
                int maxY =screenArea.Height / 2 - sceneBoard;
                if (int.Parse(this.ArrowOffsetX.Text) >= maxX)
                {
                    this.ArrowOffsetX.Text = maxX.ToString();
                }
                if (int.Parse(this.ArrowOffsetX.Text) <= -maxX)
                {
                    this.ArrowOffsetX.Text = (-maxX).ToString();

                }

                if (int.Parse(this.ArrowOffsetY.Text) >= maxY)
                {
                    this.ArrowOffsetY.Text = maxY.ToString();
                }
                if (int.Parse(this.ArrowOffsetY.Text) <= -maxY)
                {
                    this.ArrowOffsetY.Text = (-maxY).ToString();
                }
            }
            else
            {
                Console.WriteLine("输入有问题: " + textBox.Text);
            }

            
        }

        private void onAppTimer(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void OnCloseApp(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OnOnlyNum(object sender, KeyPressEventArgs e)
        {
            char key = e.KeyChar;

            if (key == 8)
            {
                e.Handled = false;
                return;
            }

            if (Char.IsNumber(key) && !Char.IsPunctuation(key) && !Char.IsControl(key))
            {
                e.Handled = false;

            }
            else
            {
                e.Handled = true;

            }
            TextBox textBox = (TextBox)sender;
            try
            {
            

                int num = int.Parse(textBox.Text);
                if (num > 0 && num<=100)
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            catch
            {
                e.Handled = true;
            }
          
        }

        private void OnOnlyNumOver(object sender, KeyEventArgs e)
        {
            TextBox textBox = (TextBox)sender;

            if (textBox.Text.Length == 0)
            {
                textBox.Text = "1";
            }
            else 
            {
                int num = int.Parse(textBox.Text);
                if (num <= 0)
                {
                    textBox.Text = "0";
                }
                else if(num>=100)
                {
                    textBox.Text = "100";
                }
            }
        }

        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            TC.Text = hScrollBar1.Value.ToString();
        }

        private void TC_TextChanged(object sender, EventArgs e)
        {
            hScrollBar1.Value = int.Parse(TC.Text);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
